package io.renren.modules.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.renren.common.exception.RenException;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.customer.dao.CustomerInfoDao;
import io.renren.modules.customer.dto.CustomerInfoDTO;
import io.renren.modules.customer.entity.CustomerInfoEntity;
import io.renren.modules.customer.service.CustomerInfoService;
import cn.hutool.core.util.StrUtil;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysRoleUserService;
import io.renren.modules.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 客户表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-12-07
 */
@Slf4j
@Service
public class CustomerInfoServiceImpl extends CrudServiceImpl<CustomerInfoDao, CustomerInfoEntity, CustomerInfoDTO> implements CustomerInfoService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<CustomerInfoEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String companyName = (String)params.get("companyName");
        String contactPerson = (String)params.get("contactPerson");
        String customerType = (String)params.get("customerType");

        QueryWrapper<CustomerInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.like(StrUtil.isNotBlank(companyName), "company_name", companyName);
        wrapper.like(StrUtil.isNotBlank(contactPerson), "contact_person", contactPerson);
        wrapper.eq(StrUtil.isNotBlank(customerType), "customer_type", customerType);

        return wrapper;
    }


    @Override
    public PageData<CustomerInfoDTO> customerPage(Map<String, Object> params) {
        log.info("客户表分页");
        // 获取所有的用户信息
        List<SysUserDTO> userDtoList = sysUserService.getAll();

        PageData<CustomerInfoDTO> page = this.page(params);
        page.getList().forEach(e -> {
            SysUserDTO creatorDto = userDtoList.stream().filter(user -> user.getId().compareTo(e.getCreator()) == 0).findFirst().orElse(null);
            e.setCreatorName(creatorDto == null ? "" : creatorDto.getRealName());
            SysUserDTO updaterDto = userDtoList.stream().filter(user -> user.getId().compareTo(e.getUpdater()) == 0).findFirst().orElse(null);
            e.setUpdaterName(updaterDto == null ? "" : updaterDto.getRealName());
        });
        return page;
    }

    @Override
    public List<CustomerInfoDTO> getAll() {
        log.info("获取所有的客户信息");
        List<CustomerInfoEntity> entityList = this.baseDao.selectList(null);
        return ConvertUtils.sourceToTarget(entityList, CustomerInfoDTO.class);
    }

    @Override
    public void customerSave(CustomerInfoDTO dto) {
        log.info("保存客户信息【{}】", dto);
        QueryWrapper<CustomerInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("company_name", dto.getCompanyName());
        List<CustomerInfoEntity> entityList = this.baseDao.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(entityList)) {
            throw new RenException("客户名称" + dto.getCompanyName() + "已存在");
        }
        this.save(dto);
    }

    @Override
    public void customerUpdate(CustomerInfoDTO dto) {
        log.info("更新客户信息");
        QueryWrapper<CustomerInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.ne("id", dto.getId());
        wrapper.eq("company_name", dto.getCompanyName());
        List<CustomerInfoEntity> entityList = this.baseDao.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(entityList)) {
            throw new RenException("客户名称" + dto.getCompanyName() + "已存在");
        }
        this.update(dto);
    }

}